package com.android.svmc.app.mobile.data;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.android.svmc.app.mobile.activity.R;

public class DataManager {
	private static String TAG = "DataManager";
	
	private static Context context;
	public static HashMap<String, Bitmap> hashMapBitmap = new HashMap<String, Bitmap>();

	public static Bitmap GetBitmap(String imgUrl) {
		if (hashMapBitmap.containsKey(imgUrl)) {
			Bitmap bitmap = hashMapBitmap.get(imgUrl);
			return bitmap;
		}
		try {
			Bitmap bm = BitmapFactory.decodeResource(context.getResources(),
					R.drawable.ic_launcher);
			hashMapBitmap.put(imgUrl, bm);
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		HttpUriRequest request = new HttpGet(imgUrl);
		HttpClient httpClient = new DefaultHttpClient();
		HttpResponse response;
		try {
			response = httpClient.execute(request);
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if (statusCode == 200) {
				HttpEntity entity = response.getEntity();
				byte[] bytes = EntityUtils.toByteArray(entity);
				Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0,
						bytes.length);

				hashMapBitmap.put(imgUrl, bitmap);
				return bitmap;
			} else {
				// throw new IOException("Download failed, HTTP response code "
				// + statusCode + " - " + statusLine.getReasonPhrase());
				Log.i(TAG, "get bitmap error: " + imgUrl);
				return hashMapBitmap.get(imgUrl);
			}

		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}

		return null;
	}


	public DataManager(Context context, String url) {
		
	}

	public DataManager(Context context, String data, boolean offline) {

	}
	@SuppressWarnings("unused")
	private String GetStringData(String url) {
		String data = null;
		try {
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(url);
			HttpResponse httpResponse = httpClient.execute(httpGet);
			HttpEntity httpEntity = httpResponse.getEntity();
			data = EntityUtils.toString(httpEntity);
		} catch (UnsupportedEncodingException e) {

		} catch (ClientProtocolException e) {

		} catch (IOException e) {

		}

		return data;
	}
}
// ///////v2